﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using LuaInterface;

public class ResourceManager {

	private static bool inited = false;
	private static bool devMode = false;

	public static void LoadConfig() {
		Debug.Log ("ResourceManager LoadConfig");
		if (inited)
			return;
		
		LuaState luaState = LuaClient.GetMainState ();
		if (luaState == null)
			return;
		
		LuaTable luaTable = luaState.GetTable ("GameConfig");
		if (luaTable != null) {
			devMode = (bool)luaTable ["devMode"];

			Debug.Log ("devMode is " + devMode);

			inited = true;

			luaTable.Dispose ();
			luaTable = null;
		}
	}

	private static ResourceConfig resourceConfig = null;
	private static ResourceUpdateFile resourceUpdateFile = null;

	public static void LoadConfigFile() {
		Debug.Log ("ResourceManager LoadConfigFile");
		if (resourceConfig == null) {
			resourceConfig = new ResourceConfig ();
			resourceConfig.Load ("ResourceConfig/resourcefile.txt");
		}
		if (resourceUpdateFile == null) {
			resourceUpdateFile = new ResourceUpdateFile ();
			resourceUpdateFile.Load ("ResourceConfig/updatefile.txt");
		}
	}

	public static void UnloadConfigFile() {
		if (resourceConfig != null) {
			resourceConfig.Unload ();
			resourceConfig = null;
		}

		if (resourceUpdateFile != null) {
			resourceUpdateFile.Unload ();
			resourceUpdateFile = null;
		}
	}

	// 从Resouce目录加载，传入的是相对路径
	private static UnityEngine.Object LoadFromResource(string path) {
		UnityEngine.Object res = Resources.Load (path);
		return res;
	}

	private static UnityEngine.Object LoadFromResourceByType(string path, Type resType) {
		UnityEngine.Object res = Resources.Load (path, resType);
		return res;
	}

	// 从AssetBundle加载，传入相对路径，根据处于读写目录进行拼接
	private static Dictionary<string, AssetBundle> dicAssetBundles = new Dictionary<string, AssetBundle>();

	public static void BeginLoadAssetBundleResouces() {
	}

	private static UnityEngine.Object LoadFromAssetBundle(string name, string path, bool isInWritePath) {
		AssetBundle assetBundle = GetAssetBundle (path);
		if (assetBundle == null) {
			string absolutePath;
			if (isInWritePath) {
				absolutePath = UnityPath.GetWritablePath (path);
			} else {
				absolutePath = UnityPath.GetStreamingAssetPath (path);
			}

			string assetBundlePath = UnityPath.GetAssetBundlePath (absolutePath);
			assetBundle = AssetBundle.LoadFromFile (assetBundlePath);
			if (assetBundle != null) {
				dicAssetBundles.Add (path, assetBundle);
			}
		}

		if (assetBundle != null) {
			UnityEngine.Object res = assetBundle.LoadAsset (name);
			return res;
		}
	
		return null;
	}

	public static void EndLoadAssetBundleResources() {
		foreach (KeyValuePair<string, AssetBundle> pair in dicAssetBundles) {
			pair.Value.Unload (false);
		}
		dicAssetBundles.Clear ();
	}

	private static AssetBundle GetAssetBundle(string path) {
		AssetBundle assetBundle = null;
		if (dicAssetBundles.TryGetValue (path, out assetBundle)) {
			return assetBundle;
		}

		return null;
	}

	public static UnityEngine.Object LoadResource(string fileName) {
		//Debug.Log ("ResourceManager LoadResource file name is " + fileName);
		string filePath;
		bool isFromBundle;
		bool isExist = resourceConfig.GetFilePath (fileName, out filePath, out isFromBundle);
		//Debug.Log ("isExist " + isExist);
		//Debug.Log ("file path is " + filePath);
		//Debug.Log ("isfrombundle is " + isFromBundle);
		if (isExist) {
			if (devMode) {
				return LoadFromResource (filePath);
			} else {
				if (isFromBundle) {
					bool isInStreamingAssets;
					if (resourceUpdateFile.GetFilePath (filePath, out isInStreamingAssets)) {
						return LoadFromAssetBundle (fileName, filePath, !isInStreamingAssets);
					} else {
						Debug.LogWarning ("updatefile.txt can't find file: " + filePath);
					}
				} else {
					return LoadFromResource (filePath);
				}
			}
		}
		return null;
	}
}
